<?php

class price_files_process extends Admin_Controller
{
	var $_table_name = PRICE_LISTS;
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('price_files.price_files_list_model');
		$col = $CI->uri->segment($CI->price_files_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->price_files_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->price_files_list_model->cache->add($k, $order[$CI->price_files_list_model->cache->load($k)]);
			} else
				$CI->price_files_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->price_files_list_model->url_prefix.'/'.$CI->price_files_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		//Check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('price_files.price_files_list_model');
		
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->price_files_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->db->delete($this->_table_name, array(PRICE_LISTS_ID=>$id));
				} 
			}
		}
		
		// message
		if ($flag)
			$CI->price_files_list_model->cache->add('msg', $CI->lang->line('list_delete_all'));
		else
			$CI->price_files_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->price_files_list_model->error_string);
		
		// go to original page
		redirect($CI->price_files_list_model->url_prefix.'/'.$CI->price_files_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		//Check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('price_files.price_files_list_model');
		$id = $CI->uri->segment($CI->price_files_list_model->item_segment);
		
		if ($CI->price_files_list_model->check_delete($id))
		{
			$CI->db->delete($this->_table_name, array(PRICE_LISTS_ID=>$id));			
			// message
			$CI->price_files_list_model->cache->add('msg', $CI->lang->line('list_delete'));
		} else
			// message
			$CI->price_files_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->price_files_list_model->error_string);
		
		// go to original page
		redirect($CI->price_files_list_model->url_prefix.'/'.$CI->price_files_list_model->start);
	}
	
	function saveItem()
	{
		global $CI;
		//Check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('price_files.price_files_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('title', 'Subject', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->price_files_detail_model->cache->add('msg', $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->price_files_detail_model->cache->add('post', $_POST);
			
			// go to original page
			redirect($CI->price_files_detail_model->url_prefix.'/'.$CI->price_files_detail_model->start.'/'.$CI->price_files_detail_model->item);
		}	
		// upload
		//$CI->load->module_language('upload_images.upload_images');
		$CI->load->library("fckeditor");
		$path = $CI->fckeditor->UploadPath.'/price_files';			
		//$path = str_replace(base_url(),"./",$path);
        $path = rtrim($_SERVER['DOCUMENT_ROOT'], "/").$path;		
		// save to price_files
		if ($CI->price_files_detail_model->hasPRICE_LISTS())
		{
			// update
            $data = array(
				PRICE_LISTS_TITLE=>$CI->input->post('title'),				
				PRICE_LISTS_STATUS=>(isset($_POST['active'])?1:0)
			);
			if ($_FILES['file_name']['tmp_name']){
				$data[PRICE_LISTS_FILE_NAME] =  $_FILES["file_name"]["name"];
				$price_get = $CI->price_files_detail_model->getPRICE_LISTS();
				$old_file = $price_get->{PRICE_LISTS_FILE_NAME};
				@unlink($path.'/'.$old_file);
			}
            $CI->db->where(PRICE_LISTS_ID, $CI->price_files_detail_model->item);
			$CI->db->update($this->_table_name, $data);
		} else {
			// insert
			$data = array(
				PRICE_LISTS_ID=>$CI->price_files_detail_model->item,
				PRICE_LISTS_TITLE=>$CI->input->post('title'),
				PRICE_LISTS_STATUS=>(isset($_POST['active'])?1:0)			
			);
			if ($_FILES['file_name']['tmp_name']){
				$data[PRICE_LISTS_FILE_NAME] =  $_FILES["file_name"]["name"];
			}
			$CI->db->insert($this->_table_name, $data);
		}
		
		if ($_FILES['file_name']['tmp_name'])
		{
			
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'gif|jpg|jpeg|png|doc|docx|xls|xlsx|pdf';
			$config['max_size'] = '40960';
			$config['max_filename'] = '100';
			$CI->load->library('upload', $config);
			if (!$CI->upload->do_upload('file_name'))
			{				
				/*$data = array('upload_data' => $CI->upload->data());  
				echo $mimetype= $data['upload_data']['file_type'];  
				echo $CI->upload->display_errors();die();*/
				set_flash_message($CI->upload->display_errors());
			} 
		}
		// end upload		
		
		// message
		$CI->price_files_detail_model->cache->add('msg', $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->price_files_detail_model->url_prefix2.'/'.$CI->price_files_detail_model->start);
	}		
}

?>